<?php
/**
 * Created by PhpStorm.
 * User: kai.qiu
 * Date: 17/3/5
 * Time: 上午11:09
 */

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator;

class VerifyUserToken
{
    public function handle($request, Closure $next)
    {

        $rule = ['token' => 'required|exists:user,token'];
        $check['token'] = request()->header('X-auth-token', '');
        $validator = Validator::make($check, $rule);

        if($validator->fails())
        {

            $errors = $validator->errors();

            $data['error_code'] = 9999;
            $data['data'] = (object)[];
            $data['error_message'] = implode(' ', $errors->all());

            return response()->json($data);
        }
        return $next($request);
    }
}